package com.yonyou.pmclouds.resetpassword.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.yht.sdk.SDKUtils;
import com.yonyou.yht.sdk.UserCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RequestMapping("/mobile/resetpassword")
@RestController
@Slf4j
public class ResetpasswordController {

    private ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value = "/sendPhoneCode")
    @ResponseBody
    public Object sendPhoneCode(@RequestParam String userMobile, @RequestParam String type, @RequestParam String picCode, @RequestParam String picKey) throws BusinessException {


        //判断用户是否已经注册
        String checkResult = UserCenter.isUserExist(userMobile);

        Map checkResultMap = null;
        try {
            checkResultMap = objectMapper.readValue(checkResult, Map.class);
        } catch (Exception e) {
            throw new BusinessException("系统忙，请稍后再试");
        }

        if (Integer.valueOf(0).equals(checkResultMap.get("status"))) {
            log.error("请求友户通接口异常，具体信息为：" + checkResultMap.get("msg"));
            throw new BusinessException("系统忙，请稍后再试");
        } else if (!Integer.valueOf(0).equals(checkResultMap.get("flag"))) {
            log.error("用户已存在，具体信息为：" + checkResultMap.get("msg"));
            String sendCodeResult = UserCenter.sendcode(userMobile, type, picKey, picCode);
            Map sendResultMap = null;
            try {
                sendResultMap = objectMapper.readValue(sendCodeResult, Map.class);
            } catch (Exception e) {
                log.error(e.getMessage());
            }

            if (Integer.valueOf(0).equals(sendResultMap.get("status"))) {
                throw new BusinessException("图形验证码错误");
            } else {
                return "发送成功";
            }

        } else {
            throw new BusinessException((String) checkResultMap.get("msg"));
        }
    }

    @RequestMapping(value = "/resetpassword")
    @ResponseBody
    public Object resetPassword(String userMobile, String verifyCode, String type, String newpass) throws BusinessException {

        Map validateResultMap;

        String sid;
        String userId;
        String validateResult = UserCenter.getUserByLoginName(userMobile);

        try {
            validateResultMap = objectMapper.readValue(validateResult, Map.class);
        } catch (Exception e) {
            throw new BusinessException("系统忙，请稍后再试");
        }
        userId = (String) ((Map) validateResultMap.get("user")).get("userId");
        validateResult = UserCenter.validateCode(type, userId, verifyCode, userMobile);


        try {
            validateResultMap = objectMapper.readValue(validateResult, Map.class);
        } catch (Exception e) {
            throw new BusinessException("系统忙，请稍后再试");
        }

        if (Integer.valueOf(0).equals(validateResultMap.get("status"))) {
            log.error("验证码校验错误，具体信息为：" + validateResultMap.get("msg"));
            throw new BusinessException("验证码校验错误");
        } else {
            sid = (String) validateResultMap.get("sid");
        }

        String resetResult = UserCenter.resetPassword(userId, SDKUtils.encodeUsingSHA(newpass), SDKUtils.encodeUsingMD5(newpass), sid);
        try {
            validateResultMap = objectMapper.readValue(resetResult, Map.class);
        } catch (Exception e) {
            throw new BusinessException("系统忙，请稍后再试");
        }

        if (Integer.valueOf(0).equals(validateResultMap.get("status"))) {
            if ("密码重复".equalsIgnoreCase(String.valueOf(validateResultMap.get("msg"))) ||
                    "900010000200006".equalsIgnoreCase(String.valueOf(validateResultMap.get("code")))) {
                throw new BusinessException("新密码不能与最近使用过的5个密码相同，请重新修改！");
            } else {
                throw new BusinessException("重置密码发生错误");
            }
        }

        return "重置成功";
    }

}
